import axios from "axios";
import store from "./store";

export default {
    instance: null,
    getBaseUrl () {
        return "http://"+store.state.intercomHost;
    },
    createNew () {
        const baseUrl = this.getBaseUrl();
        const result = axios.create({
            baseURL: baseUrl,
            headers:{
                'Content-Type' : 'application/json;charset=UTF-8',
                'Access-Control-Allow-Credentials':true,
                'Access-Control-Allow-Origin': '*',
                'Access-Control-Allow-Headers':'*',
                "Access-Control-Allow-Methods":"*",
            },
            timeout: 30000
        });
        // 添加请求拦截器，在请求头中加token
        result.interceptors.request.use(
            config => {
                return config;
            },
            error => {
                console.log(error);
                return Promise.reject(error);
            });

        this.instance = result;
        return this.instance;
    },
    update (newHost) {
        this.instance = null;
        this.instance = this.createNew()
    },
    getInstance () {
        if(this.instance === null) {
            this.instance = this.createNew()
        }
        return this.instance
    }
}
